<?php

namespace app\cdkjt\controller;


use data\model\VslCdkjtLogModel;
use data\model\VslOrderCustom;
use data\model\VslOrderCustomSign;
use data\service\cdkjt\CdkjtService;
use data\service\Config;
use think\Controller;
use think\Exception;

/**
 * Class Notify
 * @package app\cdkjt\controller
 * 海关申报异步回执api
 */
class Notify extends Controller
{
    public function index()
    {
        $xml1 = $this->request->param('message');
        if(empty($xml)){
            return;
        }
        //debugLog('海关179-----------------'.$xml1);
        $xml = stripslashes(htmlspecialchars_decode($xml1));
        $rs = $this->_xmlToArray($xml);
        $redis = $this->_redisHelper();
        $redis->lPush(CdkjtService::$queue_notify,$xml1);
        /*if($rs){
            if(isset($rs['PaymentReturn'])){
                return $this->_payment($rs);
            }elseif(isset($rs['OrderReturn'])){
                return $this->_order($rs);
            }elseif(isset($rs['SignReturn'])){
                return $this->_sign($rs);
            }
        }*/
        return 1;
    }

    /**
     * 支付申请回执接收方法
     */
    private function _payment($rs){
        $updata = [
            'status'    =>  $rs['PaymentReturn']['returnStatus'],
            'info'      =>  $rs['PaymentReturn']['returnInfo'],
            'createtime'=>  $rs['PaymentReturn']['returnTime'],
            'guid'      =>  $rs['PaymentReturn']['guid'],
            'content'   =>  json_encode($rs),
        ];
        VslCdkjtLogModel::update($updata,['payTransactionId'=>$rs['PaymentReturn']['payTransactionId'],'type'=>'payment']);
        return 1;
    }

    /**
     * 订单回执
     *
     */
    private function _order($rs){
        $updata = [
            'status'    =>  $rs['OrderReturn']['returnStatus'],
            'info'      =>  $rs['OrderReturn']['returnInfo'],
            'createtime'=>  $rs['OrderReturn']['returnTime'],
            'guid'      =>  $rs['OrderReturn']['guid'],
            'content'   =>  json_encode($rs),
        ];
        VslCdkjtLogModel::update($updata,['orderno'=>$rs['OrderReturn']['orderNo'],'type'=>'order']);
        return 1;
    }


    //179签名异步回执处理,成功则加入待上报队列

    private function _sign($rs){

        $copNo = $rs['SignReturn']['copNo'];
        $signResult = $rs['SignReturn']['signResult'];
        $status = $rs['SignReturn']['returnStatus'];
        $info       = $rs['SignReturn']['returnInfo'];
        $One = VslOrderCustomSign::where(['copNo'=>$copNo])->find();
        if(!$One){
            return;
        }

        $date = date('Y-m-d H:i:s');
        $One->status = $status;
        $One->content = $info;
        $One->updatetime = $date;
        $One->save();

        if($status == 4)            //异步回执签名成功，则加入上传队列
        {
            VslOrderCustom::update(['sign'=>$signResult,'updatetime'=>$date],['sessionID'=>$One->sessionID]);
            $redis = CdkjtService::connectRedis();
            $redis->lPush(CdkjtService::$queue_orders,$One->sessionID);
        }
    }

    //xml
    private function _xmlToArray($xml){
        $xml = simplexml_load_string($xml);
        $rs = json_encode($xml);
        return json_decode($rs,TRUE);
    }



    //redis实例
    private function _redisHelper()
    {
        $config = new Config();
        $redis_config = $config->getRedisConfig(0);
        $host = '';
        $pwd = '';
        if($redis_config){
            $host = $redis_config['host'];
            $pwd = $redis_config['pass'];
        }
        $port = $redis_config['port']??6379;
        try {
            $redis = new \Redis();
            if($host && $pwd){
                $redis->connect($host, $port);
                $redis->auth($pwd);
            }else{
                $redis = new \Redis();
                $redis->connect('127.0.0.1', $port);
            }
            return $redis;
        }catch (Exception $e){
            throw new Exception($e->getMessage());
        }
    }

}